package com.jjb.www.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.SeekBar;

import androidx.appcompat.widget.LinearLayoutCompat;

import com.jjb.www.R;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;


/**
 * Author: crg
 * time:   2023/11/23 16:53
 * des:    重写gsy播放器
 */
public class SampleCoverVideo extends StandardGSYVideoPlayer {

    private OnListener mOnListener;
    private LinearLayoutCompat mHint, mBuyCourse;
    //是否超过三分钟
    private boolean moreThreeMinFlag;
    //是否购买
    private int mBuyFlag;
    //学习进度
    private int mLearnedProgress;
    //是否可试听
    private int mIsAudition;

    public void setOnListener(OnListener onListener) {
        mOnListener = onListener;
    }

    public SampleCoverVideo(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public SampleCoverVideo(Context context) {
        super(context);
    }

    public SampleCoverVideo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setFlag(boolean flag) {
        moreThreeMinFlag = flag;
        loadView();
    }
    public void setTag() {
        mBuyCourse.setVisibility(GONE);
        mHint.setVisibility(GONE);
    }

    public void goneView(){
    }

    public void setBuyFlag(int buyFlag) {
        mBuyFlag = buyFlag;
    }

    public void setLearnedProgress(int learnedProgress) {
        mLearnedProgress = learnedProgress;
        mProgressBar.setEnabled(mLearnedProgress == 100 || mIsAudition == 1);
    }
    public void setIsAudition(int isAudition) {
        mIsAudition = isAudition;
        mProgressBar.setEnabled(mLearnedProgress == 100 || mIsAudition == 1);
    }

    @Override
    protected void touchSurfaceMoveFullLogic(float absDeltaX, float absDeltaY) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY);
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false;
    }

    @Override
    protected void init(Context context) {
        super.init(context);
        mHint = findViewById(R.id.ll_video_hint);
        mBuyCourse = findViewById(R.id.ll_video);
        Button button = findViewById(R.id.btn_buy_course);
//        Button hintButton = findViewById(R.id.btn_buy_course_hint);
        button.setOnClickListener(v -> {
            if (mOnListener == null) {
                return;
            }
            mOnListener.onBuyCourse();
        });
//        hintButton.setOnClickListener(v -> {
//            if (mOnListener == null) {
//                return;
//            }
//            mOnListener.onIsAudition();
//        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.video_layout_cover;
    }

    @Override
    protected void cloneParams(GSYBaseVideoPlayer from, GSYBaseVideoPlayer to) {
        super.cloneParams(from, to);
        SampleCoverVideo sf = (SampleCoverVideo) from;
        SampleCoverVideo st = (SampleCoverVideo) to;
        st.moreThreeMinFlag = sf.moreThreeMinFlag;
        st.mBuyFlag = sf.mBuyFlag;
        st.mLearnedProgress = sf.mLearnedProgress;
        st.mIsAudition = sf.mIsAudition;
        st.mOnListener = sf.mOnListener;
    }

    public void loadView() {
        if (moreThreeMinFlag) {
            changeUiToClear();
            mBuyCourse.setVisibility(VISIBLE);
            mBuyCourse.setOnClickListener(v -> { });
            mHint.setVisibility(GONE);
        } else {
            mBuyCourse.setVisibility(GONE);
            mHint.setVisibility(VISIBLE);
        }
    }

    @Override
    public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
        GSYBaseVideoPlayer gsyBaseVideoPlayer = super.startWindowFullscreen(context, actionBar, statusBar);
        SampleCoverVideo sampleCoverVideo = (SampleCoverVideo) gsyBaseVideoPlayer;
        //横屏也需要设置进度条是否可以改变进度
        sampleCoverVideo.mProgressBar.setEnabled(sampleCoverVideo.mLearnedProgress == 100 || sampleCoverVideo.mIsAudition == 1);
        if (sampleCoverVideo.mBuyFlag == 0) {
            sampleCoverVideo.loadView();
        }
        return gsyBaseVideoPlayer;
    }

    public interface OnListener {
        /** 试看结束购买课程 */
        void onBuyCourse();
        /** 试看过程中购买课程 */
        void onIsAudition();
    }

}
